package com.hejjon.knowledgebase.util;

import com.hejjon.knowledgebase.constant.MessageConstant;
import com.hejjon.knowledgebase.exception.InvalidArgumentException;
import com.hejjon.knowledgebase.exception.NullArgumentException;

import java.util.Collection;

/**
 * @author:
 * @date: 2022-12-08 10:13:58
 * @since: 1.0
 */
public class Valid {


    public static <T> T notNull(T object, String message) {
        if (object == null) {
            throw new NullArgumentException(message);
        } else {
            return object;
        }
    }

    /**
     * 校验非null
     * @param object
     * @param <T>
     * @return
     */
    public static <T> T notNull(T object) {
        if (object == null) {
            throw new NullArgumentException(MessageConstant.ABSENT_PARAM);
        } else {
            return object;
        }
    }


    /**
     * 校验字符串非空
     * @param s 字符串
     * @param message 提示信息
     * @return
     */
    public static String notEmpty(String s, String message) {

        if (s == null) {
            throw new NullArgumentException(message);
        } else if (s.length() == 0) {
            throw new InvalidArgumentException(message);
        }
        return s;
    }

    /**
     * 校验字符串非空
     * @param s 字符串
     * @return
     */
    public static String notEmpty(String s) {
        return notEmpty(s, MessageConstant.EMPTY_PARAMS);
    }


    /**
     * 校验集合非空
     * @param collection    集合
     * @param message   提示信息
     * @param <T>
     * @return
     */
    public static <T extends Collection<?>> T notEmpty(T collection, String message) {
        if (collection == null) {
            throw new NullArgumentException(message);
        } else if (collection.isEmpty()) {
            throw new InvalidArgumentException(message);
        } else {
            return collection;
        }
    }

    /**
     * 校验集合非空
     * @param collection    集合
     * @param <T>
     * @return
     */
    public static <T extends Collection<?>> T notEmpty(T collection) {
        return notEmpty(collection, MessageConstant.EMPTY_PARAMS);
    }




}
